<?php

namespace app\admin\controller;

use think\facade\{View, Session, Db};
use app\BaseController;
use think\exception\HttpResponseException;
class Base extends BaseController
{
    protected $Ajax=0; //0 跳转请求，1 ajax请求
    public function initialize()
    {
        parent::initialize();
        $this->Ajax = request()->isAjax() ? 1 : 0;
        if(!$this->isLogin()){
            if($this->Ajax){
                throw new HttpResponseException(returnmsg(-1,"登录超时"));
            }
            $this->redirectTo((string) url('login/login'));   
        }
    }
    private function isLogin(){
        return Session("lt_auser") ?? 0;
    }
        /**
     * 自定义重定向方法
     * @param $args
     */
    private function redirectTo(...$args)
    {
        // 此处 throw new HttpResponseException 这个异常一定要写
        throw new HttpResponseException(redirect(...$args));
    }
    protected function getVntId($uid){
        $msg=Db::name("iuser")->find($uid);
        if($msg['is_huser']){
            return $msg['id'];
        }
        if($msg['t_userId']){
            return $this->getVntId($msg['t_userId']);
        }
        return 0;

    }
    protected function getTreamUserCount($userIds,$data=[],$is=0){
        $mp=[];
        if(!$is && $data){
            $mp[]=["is_huser","=",0] ;
        }
        $userIds=Db::name("iuser")->where("t_userId","in",$userIds)->where($mp)->column("id");
        if($userIds){
             $data=array_merge($userIds,$data);
             return $this->getTreamUserCount($userIds,$data,$is);
        }
        return  $data;
    }
    protected function getTreamUserCount1($userIds,$data=[]){
        $mp=[];
        if($data){
            $mp[]=["is_huser","=",0] ;
        }
        $userIds=Db::name("iuser")->where("t_userId","in",$userIds)->where($mp)->column("id");

        if($userIds){
             $data=array_merge($userIds,$data);
             return $this->getTreamUserCount1($userIds,$data);
        }
        return  $data;
    }




}
